package com.taptech.doufu.ui.adapter;

import android.content.Intent;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.NovelEditHistoryDetailActivity;
import com.taptech.doufu.constant.ChapterType;
import com.taptech.doufu.bean.NovelEditModel;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.TimeDateFunction;
import java.util.List;

public class NovelEditHistoryAdapter extends RecyclerView.Adapter<NovelEditHistoryAdapter.Holder> {

    List<NovelEditModel> list;

    public NovelEditHistoryAdapter(List<NovelEditModel> list){
        this.list = list;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_novel_edit_history, null);
        Holder holder = new Holder(view);
        holder.tvContent = (TextView) view.findViewById(R.id.tvContent);
        holder.tvTime = (TextView) view.findViewById(R.id.tvTime);
        holder.tvCount = (TextView) view.findViewById(R.id.tvCount);
        holder.tvState = (TextView) view.findViewById(R.id.tvState);
        return holder;
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        final NovelEditModel model = getItem(position);
        if (model == null) {
            return;
        }
        int count = 0;
        if (!TextUtils.isEmpty(model.getContent())) {
            holder.tvContent.setText(model.getContent());
            count = DiaobaoUtil.getStringSizeNoNull(model.getContent());

            String content = model.getContent();

            content = content.trim()
                    .replace("\u3000", "").replace("\u3000","")
                    .replace("\n","").replace("\n","");
            if (content.length() > 10) {
                holder.tvContent.setText(content.substring(content.length()-10));
            }else{
                holder.tvContent.setText(content);
            }
        }else{
            holder.tvContent.setText("");
        }
        holder.tvCount.setText(count+"字");
        if (model.getCreated_at() > 0) {
            holder.tvTime.setText("保存时间："+TimeDateFunction.toDateTime(model.getCreated_at()*1000));
        }else{
            holder.tvTime.setText("保存时间：0000-00-00 00:00:00");
        }
        if (ChapterType.PENDING.equals(String.valueOf(model.getDraft_status()))||
                ChapterType.PENDING2.equals(String.valueOf(model.getDraft_status()))||
                ChapterType.PUBLISHED.equals(String.valueOf(model.getDraft_status()))||
                ChapterType.MODIFY.equals(String.valueOf(model.getDraft_status()))){  //审核中
            holder.tvState.setVisibility(View.VISIBLE);
        }else{
            holder.tvState.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(v.getContext(), NovelEditHistoryDetailActivity.class);
                intent.putExtra(NovelEditHistoryDetailActivity.INTENT_KEY_NOVEL_DETAIL, model);
                v.getContext().startActivity(intent);

            }
        });

    }


    private NovelEditModel getItem(int position) {
        if (list != null && list.size() > position) {
            return list.get(position);
        }
        return null;
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }



    public class Holder extends RecyclerView.ViewHolder {
        private TextView tvContent;
        private TextView tvTime;
        private TextView tvCount;
        private View itemView;
        private TextView tvState;

        public Holder(View itemView) {
            super(itemView);
            this.itemView = itemView;
        }
    }
}
